<?php

namespace app\api\controller;

use app\admin\model\Article AS ArticleMdl;

class Article extends BaseController
{

    /**
     * 文章列表
     */
    public function getArticle()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (empty($param->type)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'type 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'type' => $param->type
        ];

        $article = ArticleMdl::where($where)->order('sorting ASC,createDate DESC')->limit($start, $length)->select();
        $total = ArticleMdl::where($where)->count();

        if (empty($article)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到文章信息');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $article, $total);
    }


    /**
     * 文章详情
     */
    public function getArticleDetail()
    {
        $param = self::getHttpParam();
        if (empty($param->articleId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'articleId不能为空');
        }

        $article = ArticleMdl::where('id', $param->articleId)->find();

        if (empty($article)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到文章信息');
        }

        $article['readCount'] += 1;
        // 每查看一次，阅读数量加1
        ArticleMdl::where('id', $param->articleId)->setInc('readCount');

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $article);
    }

}